"""
#!/usr/bin/python
-*- coding:utf-8 -*-
@author  : liziying
@time    : 2022/7/7 15:35
@function: the script is used to do something.
@version : V1
@projectname : haizeiwang
"""
import random
import time

# 用户类
class User():
    def __init__(self, name, sex, blood, equip, room=None):
        self.name = name
        self.sex = sex
        self.blood = blood
        self.equip = equip
        self.room = room
    def get_equip(self):
        self.equip = random.randint(0,1)
        return self.equip
    def get_attack(self, role):
        print(f"{role.name}遭受攻击")
        role.blood -= 10
        # equip==1即有装备防弹衣
        if role.equip == 1 and role.blood >= 5:
            print(f"{role.name}有防弹衣")
            role.blood += 5
        return f"{role.name}剩余血量：{role.blood}"

# 房间类
class Room():
    member = []
    def __init__(self, roomnum):
        self.roomnum = roomnum
    def room_enter(self, role):
        # 房间内人数小于2，就可以进入房间
        if len(self.member) < 2 and role not in self.member:
            self.member.append(role)
            print(f"{role}已进入房间{self.roomnum},即将开始游戏")
        elif role in self.member:
            print(f"房间内已存在角色{role}，请重新输入")
        else:
            print(f"玩家[{role}]，非常抱歉，房间已满，您无法进入，请耐心等待他人战斗结束或选择其他房间战斗。")
        return self.member
    def room_exit(self, role):
        self.member.remove(role)
        print(f"{role}退出房间")
        return self.member
    def begin_game(self,role):
        # 如果房间人数已满就可以开始战斗了
        if len(role) == 2:
            print("开始战斗".center(33, "#"))
            return True

# 战斗场景
def fight():
    i = 1
    while 1:
        print(" "*40)
        print(f"第----<{i}>----回合".center(33))
        i = i + 1
        num1 = random.randint(1,2)
        if num1 == 1:
            p2.get_attack(p1)
        else:
            p1.get_attack(p2)
        if p1.blood <= 0 or p2.blood <= 0:
            winner = p1.name if p1.blood > 0 else p2.name
            print(f"{winner}---Win".center(40, "_"))
            break
        print(f"{p1.name}:hp--{p1.blood}    vs    {p2.name}:hp--{p2.blood}")
        time.sleep(0.5)
        print("_"*40)

# 创建角色
p1 = User("lzy", "f", 100, 0)
p2 = User("lwq", "f", 100, 1)
p3 = User("wh", "f", 100, 1)

# 创建房间
room1 = Room(random.randint(1,10))
print(f"创建房间{room1.roomnum}")



while True:
    print("菜单：按1进入房间\n     按2开始战斗\n     按q退出游戏")
    select = input("请输入你的选择：")
    if select == "1":
        roles = room1.room_enter(p1.name)
        roles = room1.room_enter(p3.name)
        roles = room1.room_exit(p3.name)
        roles = room1.room_enter(p2.name)
        print(f"房间{room1.roomnum}:玩家{roles}就位,请开始战斗!")
    # elif select == "2":
    #     roles = room1.room_exit(role.name)
    elif select == "2":
        if room1.begin_game(roles):
            fight()
        else:
            print("开始游戏失败，房间人数不符合要求，请耐心等待其他玩家进入。")
    elif select == "q":
        break
    else:
        print("输入有误，请重新输入。")